﻿using Furion.Schedule;

namespace ApiLib.Common.MyJobs;

/// <summary>
/// 任务执行器：用于处理超时和重试
/// </summary>
public class DefaultJobExecutor : IJobExecutor
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    /// <param name="jobHandler"></param>
    /// <param name="stoppingToken"></param>
    public async Task ExecuteAsync(JobExecutingContext context, IJob jobHandler, CancellationToken stoppingToken)
    {
        // Console.WriteLine(">>>" + jobHandler.GetType().Name);
        await jobHandler.ExecuteAsync(context, stoppingToken)
            .WaitAsync(TimeSpan.FromMilliseconds(5000), stoppingToken); // 设置 3 秒超时
        // await jobHandler.ExecuteAsync(context,stoppingToken).

        /*// 实现失败重试策略，如失败重试 3 次
        await Retry.InvokeAsync(async () => { await jobHandler.ExecuteAsync(context, stoppingToken); }, 3, 1000
            // 每次重试输出日志
            , retryAction: (total, times) =>
            {
                Log.Warning<MyJobExecutor>("Retrying {current}/{times} times for {context}", times, total, context);
            });*/
    }
}